/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.component.open.crypt.service;

import cn.hutool.core.codec.Base64;
import cn.hutool.crypto.SecureUtil;
import info.peigen.hotpot.common.service.base.processor.ProcessorServiceContext;
import info.peigen.hotpot.component.open.crypt.service.base.AbstractOpenService;
import info.peigen.hotpot.component.open.crypt.service.order.OpenCryptDecryptOrder;
import info.peigen.hotpot.component.open.crypt.service.order.OpenCryptEncryptOrder;
import info.peigen.hotpot.component.open.crypt.service.order.OpenCryptGenerateOrder;
import info.peigen.hotpot.component.open.crypt.service.result.OpenCryptDecryptResult;
import info.peigen.hotpot.component.open.crypt.service.result.OpenCryptEncryptResult;
import info.peigen.hotpot.component.open.crypt.service.result.OpenCryptGenerateCryptResult;
import info.peigen.hotpot.component.open.crypt.service.result.info.OpenCertCryptInfo;
import org.noear.solon.annotation.Component;

import java.nio.charset.StandardCharsets;
import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.PublicKey;

/**
 * <b>(OpenCryptService)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2022/6/13
 */
@Component
public class OpenCryptService extends AbstractOpenService {

    /**
     * 根据PartnerId生成加密数据
     *
     * @param order OpenCryptGenerateCryptOrder
     *
     * @return OpenCryptGenerateCryptResult
     */
    public OpenCryptGenerateCryptResult genCrypt(OpenCryptGenerateOrder order) {

        ProcessorServiceContext serviceContext = ProcessorServiceContext.builder().order(order).result(new OpenCryptGenerateCryptResult()).build();
        execute(serviceContext, lambda -> {
            KeyPair                      pair       = SecureUtil.generateKeyPair(order.getAlgorithm(), order.getKeySize(), order.getPartnerId().getBytes(StandardCharsets.UTF_8));
            PrivateKey                   privateKey = pair.getPrivate();
            PublicKey                    publicKey  = pair.getPublic();
            OpenCryptGenerateCryptResult result     = (OpenCryptGenerateCryptResult) lambda.getResult();
            result.setSuccessNotFee();
            result.setInfo(OpenCertCryptInfo.builder()
                    .privateKey(privateKey)
                    .publicKey(publicKey)
                    .privateKeyStr(Base64.encode(privateKey.getEncoded()))
                    .publicKeyStr(Base64.encode(publicKey.getEncoded()))
                    .build());
        });
        return (OpenCryptGenerateCryptResult) serviceContext.getResult();
    }

    /**
     * 加密
     *
     * @param order OpenCryptEncryptOrder
     *
     * @return OpenCryptEncryptResult
     */
    public OpenCryptEncryptResult encrypt(OpenCryptEncryptOrder order) {
        return null;
    }

    /**
     * 解密
     *
     * @param order OpenCryptDecryptOrder
     *
     * @return OpenCryptDecryptResult
     */
    public OpenCryptDecryptResult decrypt(OpenCryptDecryptOrder order) {
        return null;
    }
}